<?php
  class panelQuery
  {
      private $db;
      public function __construct()
      {
          $this -> db = new dataBase();
      }
      
      public function getUserServices($userId)
      {
            $sQuery = "SELECT * FROM service WHERE user_id=?";
            $result = $this -> db -> PdoQuery($sQuery, array($userId));
            
            return $result;   
      }
      
      public function getUserOffer($userId)
      {
          
      }
      
      public function getUserActivities($userId)
      {
          $query = "SELECT * FROM activities WHERE user_id=? ORDER BY id DESC";
          $result = $this -> db -> PdoQuery($query, array($userId));
          
          $wholeAct = array();
          foreach($result as $key => $value)
          {
              $wholeAct[$key]['date'] = $value['creation_date']; 
              if($value['activity_kind'] == 1)
              {
                    $wholeAct[$key]['class'] = "plus";
                    $wholeAct[$key]['cont'] = $this -> getServiceById($value['act_id']);   
                    $wholeAct[$key]['cont'] = $wholeAct[$key]['cont'][0];   
              }
              elseif($value['activity_kind'] == 2)
              {
                    $wholeAct[$key]['class'] = "gwiazda";
                    $wholeAct[$key]['cont'] = $this -> getOfferById($value['act_id']);
                    $wholeAct[$key]['cont'] = $wholeAct[$key]['cont'][0];
              }
          }
          return $wholeAct;
      }
      
      public function getUserActivitiesSearch($userId, $search)
      {
          $query = "SELECT * FROM activities WHERE user_id=? ORDER BY id DESC";
          $result = $this -> db -> PdoQuery($query, array($userId));
          
          $wholeAct = array();
          foreach($result as $key => $value)
          {
              $wholeAct[$key]['date'] = $value['creation_date']; 
              if($value['activity_kind'] == 1)
              {
                    $wholeAct[$key]['class'] = "plus";
                    $wholeAct[$key]['cont'] = $this -> getServiceByIdSearch($value['act_id'], $search);   
                    if(isset($wholeAct[$key]['cont'][0]['id']))
                    {
                        $wholeAct[$key]['cont'] = $wholeAct[$key]['cont'][0];    
                    }
                    else
                    {
                        unset($wholeAct[$key]);
                    }
                       
              }
              elseif($value['activity_kind'] == 2)
              {
                    $wholeAct[$key]['class'] = "gwiazda";
                    $wholeAct[$key]['cont'] = $this -> getOfferByIdSearch($value['act_id'], $search);
                    if(isset($wholeAct[$key]['cont'][0]['id']))
                    {
                        $wholeAct[$key]['cont'] = $wholeAct[$key]['cont'][0];    
                    }
                    else
                    {
                        unset($wholeAct[$key]);
                    }
              }
          }
          return $wholeAct;   
      }
      
      private function getServiceByIdSearch($id, $search)
      {
          $query = "SELECT * FROM service WHERE id = ? AND lower(description) LIKE ?";
          $result = $this -> db -> PdoQuery($query, array($id, '%'.strtolower($search).'%'));

          return $result;
      }
      
      private function getOfferByIdSearch($id, $search)
      {
          $query = "SELECT * FROM offers WHERE id = ? AND lower(description) LIKE ?";
          $result = $this -> db -> PdoQuery($query, array($id, '%'.strtolower($search).'%'));
          
          return $result;   
      }
      
      private function getServiceById($id)
      {
          $query = "SELECT * FROM service WHERE id = ?";
          $result = $this -> db -> PdoQuery($query, array($id));

          return $result;
      }
      
      private function getOfferById($id)
      {
          $query = "SELECT * FROM offers WHERE id = ?";
          $result = $this -> db -> PdoQuery($query, array($id));
          
          return $result;   
      }
      
      public function getMyServices($id)
      {
          $query = "SELECT * FROM service WHERE user_id=? ORDER BY creation_date DESC";
          $result = $this -> db -> PdoQuery($query, array($id));
          
          return $result;
      }
  }
?>